package org.tinygroup.dbf.impl;

import org.tinygroup.dbf.DbfDataSet;
import org.tinygroup.dbf.DbfDocument;
import org.tinygroup.dbf.DbfReader;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;

/**
 * Created by luoguo on 2014/7/10.
 */
public class MemoryDbfReader implements DbfReader {
    public DbfDocument readDbf(String fileName, String encode) throws IOException, ParseException {
        return readDbf(new File(fileName), encode);
    }

    public DbfDocument readDbf(File file, String encode) throws IOException, ParseException {
        StreamDbfReader streamDbfReader = new StreamDbfReader();
        DbfDocument dbfDocument = streamDbfReader.readDbf(file, encode);
        MemoryDbfDataSet memoryDbfDataSet = new MemoryDbfDataSet();
        DbfDataSet dataSet = dbfDocument.getDataSet();
        while (dataSet.next()) {
            memoryDbfDataSet.getDbfRecords().add(dataSet.getDbfRecord());
        }
        streamDbfReader.close();
        dbfDocument.setDataSet(memoryDbfDataSet);
        return dbfDocument;
    }

    public void close() throws IOException {

    }
}
